package com.mageex.magentoid.view;

import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.http.cookie.Cookie;

import com.ivica.magentoid.MagentoidActivity;
import com.ivica.magentoid.configuration.ds.ConfigurationDS;
import com.ivica.magentoid.core.MagentoidApp;


import android.os.AsyncTask;
import android.widget.Toast;

public class Loader extends AsyncTask<Void, Void, ConfigurationDS> {
	
	MagentoidApp mApp = MagentoidApp.getInstance();
	private ConfigurationDS ds ;
	private LoaderInterface delegate = null;
	private CountDownLatch doneSignel;
	private List<Cookie> cookies;
	private boolean showLoadingIndicator = true;
	private boolean showSuccessMessage = false;
	private boolean success = false;
	
	public static abstract interface LoaderInterface{
		public abstract void postExecute(ConfigurationDS ds, boolean paramBoolean);

	    public abstract void postExecuteInBackground(ConfigurationDS ds, boolean paramBoolean);

	    public abstract void preExecute();

	    public abstract void preExecuteInBackground();
	}
	
	
	@Override
	protected ConfigurationDS doInBackground(Void... params) {
		MagentoidActivity.logD("startProcessing...");
		
		if (this.delegate != null) 
			this.delegate.preExecuteInBackground();
		
		if (mApp.getConfiguration().ds == null) {
			MagentoidActivity.logE("Connection failed!.");
			ds = null;
		} else {
			ds =  mApp.getConfiguration().ds;
		}
		
		MagentoidActivity.logD("End proces.");
		return ds;
	}
	public List<Cookie> getCookies() {
		return this.cookies;
	}
	public CountDownLatch getDoneSignel() {
		return this.doneSignel;
	}
	public boolean isSuccess() {
		return this.success;
	}
	
	@Override
	protected void onCancelled() {
		super.onCancelled();
		this.doneSignel.countDown();
		MagentoidActivity.logD("End Loading...");
	}
}
